package cn.itcast.user.service.impl;

import cn.itcast.hmall.dto.common.ThreadLocalUtil;
import cn.itcast.hmall.pojo.order.Order;
import cn.itcast.hmall.pojo.user.Address;
import cn.itcast.hmall.pojo.user.User;
import cn.itcast.user.mapper.TbAddressMapper;
import cn.itcast.user.mapper.TbUserMapper;
import cn.itcast.user.pojo.TbAddress;
import cn.itcast.user.pojo.TbUser;
import cn.itcast.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private TbAddressMapper tbAddressMapper;
    @Autowired
    private TbUserMapper userMapper;
    /**
     * 根据uid查询数据
     * @return
     */
    @Override
    public List<Address> findAddressById() {
        Long userId = ThreadLocalUtil.getUserId();
        System.out.println(userId );
        return    tbAddressMapper.findAddressById(userId);

    }

    //根据id查数据
    @Override
    public Address findAddressByIdd(String addressId) {
        return tbAddressMapper.findById(addressId);

    }

    @Override
    public User findUserById(String id) {
        User user = userMapper.selectById(id);
        return user;
    }
        //扣钱
    @Override
    public Boolean balanceOfBuckle(Long userId, Long totalFee) {

        User user = userMapper.selectById(userId);
        Long balance = user.getBalance();
        if (balance<totalFee){
            return false;
        }
        long l = balance - totalFee;

        user.setBalance(l);
        userMapper.updateById(user);
     /*   //条件更新
        LambdaUpdateWrapper<User> wrapper = Wrappers.<User>lambdaUpdate();
        // 更新的条件
        wrapper.eq(User::getId,userId)
                // 更新的值
                .set(User::getBalance,l);*/
        return true;
    }
}
